#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use lib "/net/cpp-group/Leo/bin";
use parse_fasta;
#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888
my $usage = <<'USAGE';

USAGE:

    fa_sequence_lengths.pl
                            [--regex "(ENS[A-Z]*P\d+)"]
                            [--help]
                            sequences.fa


        This script takes files supplied via
        STDIN or on the command line and prints the fasta sequence lengths
USAGE

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory


# optional parameters
my $help = undef;
my $regex = '(.*)';
GetOptions('help' => \$help, 'regex:s' => \$regex);

die $usage if ($help);
die $usage unless $regex;

#_________________________________________________________________________________________

#   get_seq_lengths_callback
#                       $annotation     annotation line
#                       \@sequence      sequence lines
#                       $line count     line count in file
#                       $acc_pos        accession line file position
#                       $sequence_count index of current sequence
#                       $data           see below

#_________________________________________________________________________________________
sub get_seq_lengths_callback($$$$$$)
{
    my ($acc,  $seq, $save_sequences) = @_[0, 1, 5];

	my $sequence = join "", @$seq;
	$sequence =~ tr/AC_IK-NP-TVWY//cd;
	if ($acc =~ /$regex/)
	{
		print length($sequence), "\t", $1, "\n";
	}
	else
	{
		print length($sequence), "\t", $acc,"\n";
	}

}


unshift(@ARGV, '-') unless @ARGV;
while (my $ARGV = shift @ARGV)
{
	unless (open (ARGV, $ARGV))
	{
		warn "Can't open $ARGV:$!\n";
		next;
	}
	my $cnt_seq =parse_fasta::parse_sequences(  *ARGV,
												\&get_seq_lengths_callback,
												undef);
	close ARGV;
	print STDERR "$cnt_seq\tsequences parsed.\n";

}

